package myapp.Chat;


import myapp.EnumAndConstans.Constants;
import myapp.googlemap.R;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import staticClass.XmppConnection;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
	private XmppClient xmppClient = null;
	
	//private static Boolean isConnected = false;
	
	public SettingsDialog(XmppClient xmppCLient) {
		super(xmppCLient);
		// TODO Auto-generated constructor stub
		this.xmppClient = xmppCLient;
	}
	
	protected void onStart() {
        super.onStart();
        setContentView(R.layout.chat_setting_dialog);
        getWindow().setFlags(Constants.WINDOW_FLAG_FOR_DIALOG, Constants.WINDOW_MASKS_FOR_DIALOG);
        setTitle("XMPP Settings");
        Button ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(this);
    }
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ProcessIfLastUserStillConnect();
//		Log.i("SettingsDialog", "[SettingsDialog] OK clicked!");
		String host = getText(R.id.host);
		int port = Integer.parseInt(getText(R.id.port));
		String service = getText(R.id.service);
		String userEmail = getText(R.id.userid);
		String password = getText(R.id.password);
		
		// Create a connection
		Log.i("SettingsDialog", "[onClick] Start creating a connection");
		ConnectionConfiguration connConfig = new ConnectionConfiguration(host, port, service);
		XMPPConnection connection = new XMPPConnection(connConfig);
		
		Log.i("SettingsDialog", "[onClick] Start Connecting");
		
		try {
			connection.connect();
			Log.i("SettingsDialog", "[onClick] Connected to " + connection.getHost());
		} catch (XMPPException e) {
			// TODO: handle exception
			Log.e("SettingsDialog", "[onClick] Failed to connect to " + connection.getHost());
			//xmppClient.setConnection(null);
			XmppConnection.SetConnection(null);
			
			AlertDialog.Builder alert = new Builder(getContext());
			alert.setTitle("Error");
			alert.setMessage("Login failed!");
			alert.setCancelable(true);
		}
		
		try {
			XmppConnection.setUserEmailAndPassword(userEmail, password);
			connection.login(userEmail, password);
			Log.i("SettingsDialog", "[onClick]Logged in as " + connection.getUser());
			
			// Set the status to available
			Presence presence = new Presence(Presence.Type.available);
			connection.sendPacket(presence);
			//xmppClient.setConnection(connection);
			XmppConnection.SetConnection(connection);
			
		} catch (XMPPException e) {
			// TODO: handle exception
			Log.e("SettingsDialog", "[onClick] Failed to log in as " + userEmail + "Exception: " + e.getMessage());
			showLoginFailedDialog();
			//xmppClient.setConnection(null);
			XmppConnection.SetConnection(null);
		}
		dismiss();
	}

	private String getText(int id) {
		EditText widget = (EditText) this.findViewById(id);
		return widget.getText().toString();
	}
	
	private void ProcessIfLastUserStillConnect(){
		if (XmppConnection.isConnected())
			XmppConnection.ProcessOnDestroyOrStop();
	}
	
	private void showLoginFailedDialog(){
		AlertDialog.Builder alert = new AlertDialog.Builder(xmppClient);
		alert.setTitle(R.string.title_error);
		alert.setMessage(R.string.log_in_failed_message);
		alert.setIcon(R.drawable.icon_attention);
		alert.show();
	}
}
